package com.example.uidemo

import android.content.DialogInterface
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.SeekBar
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.Exception

class MainActivity : AppCompatActivity() {

    private var chinese = ""
    private var english = ""
    private var math = ""
    private var sum = "展示"
    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        buttonLeft.setOnClickListener {
            textViewDisplay.text = getString(R.string.button_left)
        }
        buttonRight.setOnClickListener {
            textViewDisplay.text = getString(R.string.button_right)
        }

        switch1.setOnCheckedChangeListener { _, b ->
            if (b) {
                textViewDisplay.text = "开"
            } else {
                textViewDisplay.text = "关"
            }
        }
        buttonOk.setOnClickListener {
            val tmp = editTextTextValue.text.toString()

            try {
                val value = tmp.toInt()
                if (value > 100) {
                    val alertDialog = AlertDialog.Builder(this)
                    alertDialog.apply {
                        setMessage("请输入小于100大于0的数")
                        setPositiveButton(
                            "确定"
                        ) { _, _ ->
                            editTextTextValue.text = null
                            Toast.makeText(applicationContext, "好的", Toast.LENGTH_SHORT).show()
                        }
                        setNeutralButton("取消", null)
                        create()
                        show()
                    }
                } else {
                    progressBarDyn.setProgress(value, true)
                    textViewDisplay.text = value.toString()
                }
            } catch (e:Exception) {
                val alertDialog = AlertDialog.Builder(this)
                alertDialog.apply {
                    setMessage("输入不合法")
                    setPositiveButton("确定") {_, _ ->
                        editTextTextValue.text = null
                        Toast.makeText(applicationContext, "好的", Toast.LENGTH_SHORT).show()
                    }
                    setNeutralButton("取消", null)
                    create()
                    show()
                }
            }

//            if (TextUtils.isEmpty(value)) {
//                value = "0"
//            }


        }

        radioGroup.setOnCheckedChangeListener { _, i ->
            when(i) {
                R.id.radioButtonAndroid -> imageViewLogo.setImageResource(R.drawable.android)
                R.id.radioButtonIOS -> imageViewLogo.setImageResource(R.drawable.ios)
            }
        }

        seekBar.setOnSeekBarChangeListener(
            object : SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                    textViewDisplay.text = p1.toString()
                }

                override fun onStartTrackingTouch(p0: SeekBar?) {

                }

                override fun onStopTrackingTouch(p0: SeekBar?) {

                }
            }
        )
        checkBoxChinese.setOnCheckedChangeListener { _, b ->
            chinese = if (b) {
                getString(R.string.chinese_checkBox)
            } else {
                ""
            }
            sum = chinese + english + math
            textViewDisplay.text = if (sum == "") getString(R.string.display) else sum
        }
        checkBoxEnglish.setOnCheckedChangeListener { _, b ->
            english = if (b) {
                getString(R.string.english_checkBox)
            } else {
                ""
            }
            sum = chinese + english + math
            textViewDisplay.text = if (sum == "") getString(R.string.display) else sum
        }

        checkBoxMath.setOnCheckedChangeListener { _, b ->
            math = if (b) {
                getString(R.string.math_checkBox)
            } else {
                ""
            }
            sum = chinese + english + math
            textViewDisplay.text = if (sum == "") getString(R.string.display) else sum
        }
        ratingBar.setOnRatingBarChangeListener { _, fl, _ ->
//            Toast.makeText(applicationContext, fl.toString(), Toast.LENGTH_SHORT).show()
            val res = fl.toString() + "星"
            textViewDisplay.text = res
        }
    }
}